package com.yzg.executors;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author misterWei
 * @create 2020年06月13号:18点34分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.WorkerThread")
public class WorkerThread {

    /**
     * 造成饥饿
     * @param args
     */
    public static void main(String[] args) {
        List<String> settings = Arrays.asList("烤鸭肠", "红烧里脊", "糖醋排骨");
        Random random = new Random();
        //做菜的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        //吃菜的线程池
        ExecutorService executorService2 = Executors.newFixedThreadPool(1);

        //厨师一正在做菜
        executorService.execute(() -> {
            String build = settings.get(random.nextInt(3));
            log.info("正在做{}", build);
            //另一个工作线程需要吃菜
            Future<?> future3 = executorService2.submit(() -> {
                return settings.get(random.nextInt(3));
            });
            try {
                log.info("吃菜{}",future3.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

        //厨师二 正在做菜
       executorService.execute(() -> {
            String build = settings.get(random.nextInt(3));
            log.info("正在做{}", build);

           //另一个工作线程需要吃菜
            Future<?> future3 = executorService2.submit(() -> {
                 return settings.get(random.nextInt(3));
            });
           try {
               log.info("吃菜{}",future3.get());
           } catch (InterruptedException | ExecutionException e) {
                  e.printStackTrace();
           }
       });

    }
}
